<template>
  <div class="models-settings">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <el-tabs v-model="activeTab" @tab-click="handleTabClick">
            <el-tab-pane>
              <template #label>
                全部 <el-tag size="small" type="info">{{ totalCount }}</el-tag>
              </template>
            </el-tab-pane>
            <el-tab-pane>
              <template #label>
                大语言模型 <el-tag size="small" type="info">{{ llmCount }}</el-tag>
              </template>
            </el-tab-pane>
            <el-tab-pane>
              <template #label>
                向量模型 <el-tag size="small" type="info">{{ embeddingCount }}</el-tag>
              </template>
            </el-tab-pane>
            <el-tab-pane>
              <template #label>
                重排序模型 <el-tag size="small" type="info">{{ rerankCount }}</el-tag>
              </template>
            </el-tab-pane>
          </el-tabs>
          <el-button type="primary" @click="handleAddModel">添加模型</el-button>
        </div>
      </template>
      <div class="search-bar">
        <el-input
          v-model="searchQuery"
          placeholder="请输入模型名称或厂商搜索"
          class="search-input"
          clearable
          @clear="handleSearch"
          @input="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
      <el-table :data="filteredModels" style="width: 100%" empty-text="暂无数据">
        <el-table-column prop="name" label="模型名称" />
        <el-table-column prop="type" label="模型类型" />
        <el-table-column prop="provider" label="提供商" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
              {{ scope.row.status === 'active' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="modelForm" label-width="120px">
        <el-form-item label="模型名称">
          <el-input v-model="modelForm.name" />
        </el-form-item>
        <el-form-item label="模型类型">
          <el-select v-model="modelForm.type" placeholder="请选择模型类型" :disabled="activeTab !== 'all'">
            <el-option label="大语言模型" value="llm" />
            <el-option label="向量模型" value="embedding" />
            <el-option label="重排序模型" value="rerank" />
          </el-select>
        </el-form-item>
        <el-form-item label="提供商">
          <el-select v-model="modelForm.provider" placeholder="请选择提供商">
            <el-option label="OpenAI" value="openai" />
            <el-option label="Anthropic" value="anthropic" />
            <el-option label="Google" value="google" />
          </el-select>
        </el-form-item>
        <el-form-item label="API Key">
          <el-input v-model="modelForm.apiKey" type="password" show-password />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="modelForm.status" :active-value="'active'" :inactive-value="'inactive'" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, computed } from 'vue';
import { useModelsStore } from '@/store/models';
import type { Model } from '@/store/models';
import { Search } from '@element-plus/icons-vue';

export default defineComponent({
  name: 'ModelsSettings',
  components: {
    Search
  },
  setup() {
    const modelsStore = useModelsStore();
    const dialogVisible = ref(false);
    const dialogTitle = ref('添加模型');
    const models = ref<Model[]>([]);
    const activeTab = ref('all');
    const searchQuery = ref('');

    const totalCount = computed(() => models.value.length);
    const llmCount = computed(() => models.value.filter(m => m.type === 'llm').length);
    const embeddingCount = computed(() => models.value.filter(m => m.type === 'embedding').length);
    const rerankCount = computed(() => models.value.filter(m => m.type === 'rerank').length);

    const modelForm = reactive<Model>({
      id: '',
      name: '',
      type: '',
      provider: '',
      apiKey: '',
      status: 'active' as 'active' | 'inactive'
    });

    const filteredModels = computed(() => {
      let result = models.value;
      
      // 根据标签页过滤
      if (activeTab.value !== 'all') {
        result = result.filter(model => model.type === activeTab.value);
      }
      
      // 根据搜索关键词过滤
      if (searchQuery.value) {
        const query = searchQuery.value.toLowerCase();
        result = result.filter(model => 
          model.name.toLowerCase().includes(query) || 
          model.provider.toLowerCase().includes(query)
        );
      }
      
      return result;
    });

    const handleSearch = () => {
      // 搜索逻辑已经在 computed 中实现
    };

    const handleTabClick = () => {
      // 切换标签时重置表单
      modelForm.type = activeTab.value === 'all' ? '' : activeTab.value;
    };

    const handleAddModel = () => {
      dialogTitle.value = '添加模型';
      modelForm.id = '';
      modelForm.name = '';
      modelForm.type = activeTab.value === 'all' ? '' : activeTab.value;
      modelForm.provider = '';
      modelForm.apiKey = '';
      modelForm.status = 'active';
      dialogVisible.value = true;
    };

    const handleEdit = (row: any) => {
      dialogTitle.value = '编辑模型';
      Object.assign(modelForm, row);
      dialogVisible.value = true;
    };

    const handleDelete = async (row: any) => {
      await modelsStore.deleteModel(row.id);
      await fetchModels();
    };

    const handleSave = async () => {
      if (modelForm.id) {
        await modelsStore.updateModel(modelForm);
      } else {
        await modelsStore.createModel(modelForm);
      }
      dialogVisible.value = false;
      await fetchModels();
    };

    const fetchModels = async () => {
      models.value = await modelsStore.fetchModels();
    };

    fetchModels();

    return {
      dialogVisible,
      dialogTitle,
      models,
      modelForm,
      activeTab,
      filteredModels,
      totalCount,
      llmCount,
      embeddingCount,
      rerankCount,
      searchQuery,
      handleSearch,
      handleTabClick,
      handleAddModel,
      handleEdit,
      handleDelete,
      handleSave
    };
  }
});
</script>

<style lang="scss" scoped>
.models-settings {
  padding: 20px;

  .box-card {
    margin-bottom: 20px;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  :deep(.el-tabs__header) {
    margin-bottom: 0;
  }

  :deep(.el-tabs__item) {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .search-bar {
    margin: 16px 0;
    
    .search-input {
      width: 300px;
    }
  }
}
</style> 